// pages/order/order.js
var app = getApp()
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		isAuthorize: false,
		tabState: '',
		userInfo: {},
		tab1: {
			list: [
				// {
				// 	id: 'tomeal',
				// 	name: '待取餐',
				// },
				{
					id: 'topay',
					name: '已完成',
				},
				{
					id: 'remove',
					name: '待付款',
				},
				{
					id: 'pay',
					name: '已支付',
				},
				// {
				// 	id: 'meal',
				// 	name: '已取餐',
				// },
			],
			selectedId: 'tomeal',
		},
		list: [], //订单列表
		imgUrl: '', //图片地址
		deleteStatus: false, //删除状态
		hidden: 'false',
		typesNum: 5,
		status: '7',
		logfee: '',
		textareaInput: '',
		order: '',
		orderTitle: '',
		totalprice: '',
		realMoney: '',
		addressid: '',
		consumeMoney: '',
	},
	getuserInfo1: function(e) {
		wx.getUserInfo({
			success: res => {
				app.getOpenid()
				setTimeout(() => {
					this.getOrderData(7)
				}, 3500)

				this.setData({
					isAuthorize: true,
				})
			},
		})
	},
	/**
	 * tab点击触发事件
	 */
	changeMeal: function(e) {
		let val
		let _this = this
		if (e == undefined) {
			val = 'tomeal'
		} else {
			val = e.detail
		}
		this.setData({
			tabState: val,
		})
		switch (val) {
			case 'tomeal':
				_this.setData({
					deleteStatus: true,
				})
				_this.getOrderData(7)
				break
			case 'topay':
				_this.setData({
					deleteStatus: false,
				})
				_this.getOrderData(10)
				break
			case 'remove':
				_this.setData({
					deleteStatus: false,
				})
				_this.getOrderData(6)
				break
			case 'pay':
				_this.setData({
					deleteStatus: true,
				})
				_this.getOrderData(5)
				break
			case 'meal':
				_this.setData({
					deleteStatus: false,
				})
				_this.getOrderData(8)
				break
		}
	},

	/**
	 * 获得数据
	 */
	getOrderData: function(status) {
		var userid = app.globalDataO.openid.objectSid
		let _this = this
		app.gets(
			app.url.order.select + '?wechatId=' + userid + '&status=' + status,
		).then(re => {
			console.log(re.data)
			_this.setData({
				imgUrl: re.data.urls,
				list: re.data.billHisLine,
				status: re.data.status,
				hidden: 'true',
			})
		})
	},
	//打电话
	phone(e) {
		var phone = e.currentTarget.dataset.phone
		wx.makePhoneCall({
			phoneNumber: phone,
		})
	},
	//评论
	comment_tab(e) {
		var billid = e.currentTarget.dataset.billid
		var resid = e.currentTarget.dataset.resid
		console.log(billid, resid)
		wx.navigateTo({
			url:
				'../../pages/comment/comment?billid=' +
				billid +
				'&resid=' +
				resid,
		})
	},
	/**
	 * 点击查看详情
	 */
	PaymentCompletion: function(e) {
		let tab = this.data.tabState
		console.log(e.currentTarget.dataset.id)
		if (tab == 'topay') {
			wx.navigateTo({
				url:
					'../not_order_details/details?status=' +
					true +
					'&id=' +
					e.currentTarget.dataset.id,
			})
		} else if (tab == 'meal') {
			wx.navigateTo({
				url:
					'../end_order_details/details?id=' +
					e.currentTarget.dataset.id,
			})
		} else {
			wx.navigateTo({
				url:
					'../not_order_details/details?status=' +
					false +
					'&id=' +
					e.currentTarget.dataset.id,
			})
		}
	},
	/**
	 * 删除商品
	 */
	_deleteData: function(e) {
		var status = this.data.status
		let _this = this
		console.log(status)
		wx.showModal({
			title: '确定删除吗',
			success(res) {
				if (res.confirm) {
					console.log(e.currentTarget.dataset.id)
					app.gets(
						app.url.order.delete +
							'?billHisId=' +
							e.currentTarget.dataset.id +
							'&status=' +
							status,
					).then(re => {
						wx.showToast({
							title: '删除成功',
							icon: 'success',
							duration: 2000,
						})
						_this.getOrderData(status)
					})
					_this.getOrderData(status)
				}
			},
		})
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.getSetting()
	},
	getSetting() {
		var that = this
		wx.getSetting({
			success: function(res) {
				if (res.authSetting['scope.userInfo']) {
					console.log('进来')
					that.changeMeal()
				} else {
					console.log('没授权')
					that.setData({
						hidden: 'true',
					})
				}
			},
		})
	},
	wxPay(e) {
		var that = this
		console.log(e.currentTarget.dataset.id)
		app.post(app.url.order.cash, {
			billHisId: e.currentTarget.dataset.id,
		}).then(res => {
			console.log(res)
			var addressid = res.data.addressId
			var consumeMoney = res.data.consumeMoney
			var logfee = res.data.logfee
			var order = res.data.payOrderId
			var orderTitle = res.data.payOrderTitle
			var realMoney = res.data.realMoney
			var textareaInput = res.data.decsription
			this.pay(
				addressid,
				consumeMoney,
				logfee,
				order,
				orderTitle,
				realMoney,
				textareaInput,
			)
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {},
	//支付
	pay(
		addressid,
		consumeMoney,
		logfee,
		order,
		orderTitle,
		realMoney,
		textareaInput,
	) {
		let openid = app.globalDataO.openid.openid
		var that = this
		app.post(app.url.wxPay1, {
			openid: openid,
			payOrderTitle: orderTitle,
			payOrderId: order,
			realMoney: realMoney,
			logfee: logfee,
			addressId: addressid,
			consumeMoney: consumeMoney,
			decsription: textareaInput,
		}).then(res => {
			console.log(res)
			let nonceStr = res.data.data.nonceStr
			let packages = res.data.data.package
			let paysign = res.data.data.paySign
			let timeStamp = res.data.data.timeStamp
      let signtype = res.data.data.signType
			wx.requestPayment({
				timeStamp: timeStamp,
				nonceStr: nonceStr,
				paySign: paysign,
				signType: signtype,
				package: packages,
				success(res) {
					console.log(res)
					wx.showToast({
						title: '支付成功',
					})
					that.setData({
						['tab1.selectedId']: 'pay',
						status: 5,
					})
					that.getOrderData(5)
				},
				fail() {
					wx.showToast({
						image: '../../images/gantan.png',
						title: '支付取消',
					})
				},
			})
		})
	},
	/**
	 *
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		var that = this
		wx.getSetting({
			success: function(res) {
				if (res.authSetting['scope.userInfo']) {
					that.setData({
						isAuthorize: true,
					})
					that.orderType()
				} else {
					that.setData({
						isAuthorize: false,
					})
				}
			},
		})
	},
	//接受订单类型
	orderType() {
		var value = wx.getStorageSync('ordetType')
		console.log(value)
		if (
			JSON.parse(value).status != 'undefind' &&
			JSON.parse(value).name != 'undefind'
		) {
			var status = JSON.parse(value).status
			var name = JSON.parse(value).name
		}
		this.setData({
			['tab1.selectedId']: name,
			status: status,
		})
		console.log(this.data.status)
		this.getOrderData(this.data.status)
	},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {},
})
